package de.fhaugsburg.games.boardgames.scrabble.wordlist;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.LetterPiece;

public class WordArrayList implements IWordList {

	private List<String> wordList;
	
	
	
	public WordArrayList() {
		wordList = new ArrayList<String>();
		loadList();
	}
	
	
	/**
	 * fills the List
	 */
	private void loadList(){
//		wordList.add("ER");
//		wordList.add("ES");
//		wordList.add("DER");
//		wordList.add("DIE");
//		wordList.add("DAS");
//		wordList.add("SEHR");
//		wordList.add("WENIG");
//		wordList.add("AM");
//		wordList.add("UM");
//		wordList.add("NUR");
//		wordList.add("ABC");
//		wordList.add("AB");
//		wordList.add("UN");
//		wordList.add("NU");
//		wordList.add("BU");
		try {	
			//File list = new File (System.getProperty("user.dir")  + "/resource/wordlists/wordlist_small.txt");
//			InputStream list = new File (this.getClass().getResource("/resource/wordlists/wordlist_small.txt").openStream());
			InputStream list = this.getClass().getResource("/resource/wordlists/wordlist_small.txt").openStream();
			Scanner scanner = new Scanner(list).useDelimiter("[^A-Za-z|Ä|ä|Ü|ü|Ö|ö]+");
			while(scanner.hasNext()) {
				wordList.add(scanner.next().toUpperCase());
			}
			scanner.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/* (non-Javadoc)
	 * @see de.fhaugsburg.games.boardgames.scrabble.wordlist.IWordList#getList()
	 */
	public List<String> getList(){
		return wordList;
	}
	
	
	/**
	 * string contained in list?
	 * @param word
	 * @return
	 */
	public boolean contains(String word){
		return wordList.contains(word);
	}
	
	
	/**
	 * returns if given letterPieces represent a word of this list
	 * @param pieceSet
	 * @return
	 */
	public boolean contains(Set<LetterPiece> pieceSet){
		return contains(pieceSet.toString());
	}

	
	/* (non-Javadoc)
	 * @see de.fhaugsburg.games.boardgames.scrabble.wordlist.IWordList#contains(java.util.List)
	 */
	public boolean contains(List<String> words){
		if (wordList.containsAll(words)) return true;
		return false;
	}
		
	/* (non-Javadoc)
	 * @see de.fhaugsburg.games.boardgames.scrabble.wordlist.IWordList#containsPrefix(java.lang.String)
	 */
	public boolean containsPrefix(String prefix){
		for (String word: wordList){
			if (word.toLowerCase().startsWith(prefix.toLowerCase())){
				return true;
			}
		}
		return false;
	}
	
	
	/* (non-Javadoc)
	 * @see de.fhaugsburg.games.boardgames.scrabble.wordlist.IWordList#containsPrefix(java.util.List)
	 */
	public boolean containsPrefix(List<String> prefix){
		if (prefix.size() == 0) return false;
		for (String string : prefix) {
			if (!containsPrefix(string)) return false;
		}
		return true;
	}
	
	
}
